package com.wgj.android.widget;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiaai.xui.utils.XEmptyUtils;
import com.wgj.android.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/1.
 */

public class NumberEditView extends LinearLayout{
    @BindView(R.id.number)
    TextView numberEdit;

    private  NumberListener numberListener;

    public NumberEditView(Context context) {
        this(context,null);
    }

    public NumberEditView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public NumberEditView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view= LayoutInflater.from(context).inflate(R.layout.number_edit_view,this);
        ButterKnife.bind(view);
    }

    @OnClick(R.id.addBut) void  onAddClick(View view){
        int oldVlaue= XEmptyUtils.isSpace(numberEdit.getText().toString())?0:Integer.valueOf(numberEdit.getText().toString());
        oldVlaue+=1;
        numberListener.onValueChanged(oldVlaue);
        numberEdit.setText(oldVlaue+"");

    }
    @OnClick(R.id.subtracBut) void  onSubtracButClick(View view){
        int oldVlaue= XEmptyUtils.isSpace(numberEdit.getText().toString())?1:Integer.valueOf(numberEdit.getText().toString());
        oldVlaue=oldVlaue==1?1:oldVlaue-1;
        numberListener.onValueChanged(oldVlaue);
        numberEdit.setText(oldVlaue+"");

    }

    public  int getValue(){
        return  XEmptyUtils.isSpace(numberEdit.getText().toString())?0:Integer.valueOf(numberEdit.getText().toString());
    }

    public  void  setValue(int value){
        numberEdit.setText(value+"");
    }

    public void setNumberListener(NumberListener numberListener) {
        this.numberListener = numberListener;
    }

    public  interface  NumberListener{
        public  void onValueChanged(int value);

    }
}
